package com.wxproject.msg.template;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.publicapi.AccessTokenApi;
import com.util.APIStart;
import com.util.ApiResult;
import com.util.HttpUtils;

/**
 * @author wangkai
 *
 *         微信模板消息api
 */
public class TemplateMsgApi {

	private static String setIndustryUrl = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=";
	private static String getIndustryUrl = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=";
	private static String addTemplateUrl = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=";
	private static String allPrivateTemplateUrl = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=";
	private static String delPrivateTemplateUrl = "https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token=";
	private static String sendUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";

	/**
	 * 设置行业
	 * 
	 * @param jsonStr
	 *               {           "industry_id1":"1",
	 *                      "industry_id2":"4"        }
	 * @return
	 */
	public static ApiResult setIndustry(String jsonStr) {
		try {
			HttpUtils.POSTHTTPS(setIndustryUrl
					+ AccessTokenApi.getAccessTokenApi().getAccessToken(),
					jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.toString());
		}
		return new ApiResult(APIStart.S00, "设置成功！");
	}

	/**
	 * 获取设置行业
	 * 
	 * @param jsonStr
	 * @return { "primary_industry":{"first_class":"运输与仓储","second_class":"快递"},
	 *         "secondary_industry"
	 *         :{"first_class":"IT科技","second_class":"互联网|电子商务"} }
	 */
	public static ApiResult getIndustry() {
		String jsonResult = null;
		try {
			jsonResult = HttpUtils.GETHTTPS(getIndustryUrl
					+ AccessTokenApi.getAccessTokenApi().getAccessToken());
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.toString());
		}
		return new ApiResult(APIStart.S00, "获取设置成功！", jsonResult);
	}

	/**
	 * 获取模板id
	 * 
	 * @param jsonStr
	 *            {            "template_id_short":"TM00015"        }
	 * @return
	 */
	public static ApiResult getTemplateId(String jsonStr) {
		String jsonResult = null;
		try {
			jsonResult = HttpUtils.POSTHTTPS(addTemplateUrl
					+ AccessTokenApi.getAccessTokenApi().getAccessToken(),
					jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.toString());
		}
		if (jsonResult != null) {
			JSONObject jsonObject = JSON.parseObject(jsonResult);
			Integer code = jsonObject.getInteger("errcode");
			String msg = jsonObject.getString("errmsg");
			if (code.intValue() == 0 && msg.trim().equals("ok")) {
				return new ApiResult(APIStart.S00, "获取成功！",
						jsonObject.getString("template_id"));
			}
		}
		return new ApiResult(APIStart.S01, "获取失败！");
	}

	/**
	 * 获取模板列表
	 * 
	 * @return
	 */
	public static ApiResult getTemplateList() {
		String jsonResult = null;
		try {
			jsonResult = HttpUtils.GETHTTPS(allPrivateTemplateUrl
					+ AccessTokenApi.getAccessTokenApi().getAccessToken());
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.toString());
		}
		return new ApiResult(APIStart.S00, jsonResult);

	}

	/**
	 * 删除模板
	 * 
	 * @param json
	 *            {"template_id" : "Dyvp3-Ff0cnail_CDSzk1fIc6-9lOkxsQE7exTJbwUE"
	 *            }
	 * @return ApiResult
	 */
	public static ApiResult delete(String jsonStr) {
		String jsonResult = null;
		try {
			jsonResult = HttpUtils.POSTHTTPS(delPrivateTemplateUrl
					+ AccessTokenApi.getAccessTokenApi().getAccessToken(),
					jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.toString());
		}
		if (jsonResult != null) {
			JSONObject jsonObject = JSON.parseObject(jsonResult);
			Integer code = jsonObject.getInteger("errcode");
			String msg = jsonObject.getString("errmsg");
			if (code.intValue() == 0 && msg.trim().equals("ok")) {
				return new ApiResult(APIStart.S00, "删除成功！");
			}
		}
		return new ApiResult(APIStart.S01, "刪除失败！");
	}

	/**
	 * 发送模板消息
	 * 
	 * @param jsonStr
	 * @return {ApiResult}
	 */
	public static ApiResult send(String jsonStr) {
		String jsonResult = null;
		try {
			jsonResult = HttpUtils.POSTHTTPS(sendUrl
					+ AccessTokenApi.getAccessTokenApi().getAccessToken(),
					jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.toString());
		}
		if (jsonResult != null) {
			JSONObject jsonObject = JSON.parseObject(jsonResult);
			Integer code = jsonObject.getInteger("errcode");
			String msg = jsonObject.getString("errmsg");
			if (code.intValue() == 0 && msg.trim().equals("ok")) {
				return new ApiResult(APIStart.S00, "发送成功！");
			}
		}
		return new ApiResult(APIStart.S01, "发送失败！");
	}

}
